package September10;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ConsoleToFile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String filePath = "console_output.txt"; // 输出文件的路径
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            while (true) {
                String input = scanner.nextLine();
                if ("quit".equalsIgnoreCase(input)) {
                    System.out.println("Exiting program.");
                    break;
                }
                // 获取当前时间
                String currentTime = timeFormat.format(new Date());
                // 将输入和时间戳写入文件
                writer.write(currentTime + " " + input);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    }
}
